package com.itluma.isWOak.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.mapper.ModuleMapper;
import com.itluma.isWOak.mapper.RoleMapper;
import com.itluma.isWOak.mapper.RoleModuleMapper;
import com.itluma.isWOak.pojo.ModuleInfo;
import com.itluma.isWOak.pojo.RoleInfo;
import com.itluma.isWOak.service.RoleService;
import com.itluma.isWOak.vo.RolesForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 飞鸟
 * @create 2021-04-07 10:14
 */
@Service
public class RoleServiceImpl implements RoleService{

    //日志
    private Logger log = LoggerFactory.getLogger (RoleServiceImpl.class);

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleModuleMapper roleModuleMapper;

    @Autowired
    private ModuleMapper moduleMapper;

    /**
     * 利用mapper子查询进行分页管理
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<RoleInfo> page(Integer page, Integer limit){
        PageHelper.startPage (page, limit);
        List<RoleInfo> roles = roleModuleMapper.list (null);

        PageInfo<RoleInfo> pageInfo = new PageInfo<> (roles);

        return pageInfo;
    }

    @Override
    public RoleInfo getByRid(String id){
        List<RoleInfo> list = roleModuleMapper.list (id);
        if (list != null && list.size () != 0) {
            return list.get (0);
        }
        return null;
    }

    /**
     * 添加角色以及权限
     *
     * @param rolesForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(RolesForm rolesForm){

        RoleInfo roleInfo = new RoleInfo ();

        roleInfo.setName (rolesForm.getRoleName ());
        //添加角色
        log.info ("service中add的数据: " + roleInfo);

        int rows1 = roleMapper.insert (roleInfo);

        if (rows1 > 0) {
            //更加权限名查找mid
            String[] name = rolesForm.getName ();

            log.info ("roleMapper添加成功后运行moduleMapper: " + rolesForm);
            List<ModuleInfo> moduleList = moduleMapper.findByName (name);

            //批量插入数据
            log.info ("roleModuleMapper批量插入的数据: " + moduleList);
            log.info ("roleModuleMapper批量插入的数据rid: " + roleInfo);

            if (moduleList != null && moduleList.size () > 0) {
                return roleModuleMapper.insertAll (moduleList, roleInfo.getRoleId ()) > 0;
            }
        }
        return false;
    }

    /**
     * 删除
     *
     * @param rid
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteByRid(String rid){

        log.info ("进入deleteByRid删除页面 : " + rid);
        //先删除rolemodule表数据

        int rows = roleModuleMapper.delete (rid);

        //再删除role表中数据
        return roleMapper.deleteByRid (rid) > 0;

    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean update(RolesForm rolesForm){

        log.info ("进入service修改页面: " + rolesForm);

        int rows1 = roleMapper.updateNameByRid (rolesForm);

        //先删除表中数据
        int rows2 = roleModuleMapper.delete (rolesForm.getRoleId ());

        String[] name = rolesForm.getName ();
        List<ModuleInfo> moduleList = moduleMapper.findByName (name);
        //再批量插入数据
        if (moduleList != null && moduleList.size () > 0) {
            return roleModuleMapper.insertAll (moduleList, Integer.valueOf (rolesForm.getRoleId ())) > 0;
        }
        //todo 判断是否更新成功 以及删除成功
        return true;
    }
    @Override
    public List<RoleInfo> list() {
        List<RoleInfo> infoList = roleMapper.list();
        return infoList;
    }
}
